//
//  DYZMyCollectController.m
//  DYZCars
//
//  Created by MF on 16/8/19.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZMyCollectController.h"
#import "DYZTopView.h"
#import "UIColor+Addition.h"
#import "DYZShowController.h"

@interface DYZMyCollectController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *labelArr;
@property (nonatomic, strong) UIImageView *imageView;

@end

static NSString *collectionViewCellID = @"collectionViewCellID";

@implementation DYZMyCollectController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}



- (void)setupUI
{
    
    DYZTopView *topView = [DYZTopView topView];
    topView.backgroundColor = [UIColor colorWithHex:0x0073dd];
    
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.right.offset(0);
        make.height.offset(56);
    }];
    
    //    topView.NewLaunchLabel = [UILabel labelWithText:@"新发布" andTextColor:[UIColor colorWithHex:0x0073dd] andFontSize:18];
    topView.NewLaunchLabel.text = @"车源";
    topView.NewLaunchLabel.textColor = [UIColor colorWithHex:0x0073dd];
    topView.NewLaunchLabel.font = [UIFont systemFontOfSize:18];
    //    topView.DealedLabel = [UILabel labelWithText:@"已成交" andTextColor:[UIColor colorWithHex:0x67676] andFontSize:14];
    topView.DealedLabel.text = @"寻车";
    topView.DealedLabel.textColor = [UIColor colorWithHex:0x676767];
    topView.DealedLabel.font = [UIFont systemFontOfSize:14];
    
    NSArray *labelArr= @[topView.NewLaunchLabel, topView.DealedLabel];
    for(int i=0; i<labelArr.count; i++)
    {
        //创建手势并添加到label
        UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        
        [labelArr[i] addGestureRecognizer:tap];
    }
    
    //添加label点击后下方的滚动条
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"my_select1"]];
    [topView.bottomView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.offset(0);
        make.height.offset(15);
    }];
    
    //开启交互
    topView.NewLaunchLabel.userInteractionEnabled = true;
    topView.DealedLabel.userInteractionEnabled = true;
    
    //全局赋值
    self.topView = topView;
    self.labelArr = labelArr;
    self.imageView = imageView;
    
    [self setupCollectionView];
}



- (void)tapGesture: (UITapGestureRecognizer *)tapGesture
{
    //    NSLog(@"%@",tapGesture.view);
    for(int i=0; i<self.labelArr.count; i++)
    {
        UILabel *label = self.labelArr[i];
        NSInteger index = (tapGesture.view.frame.origin.x * 2) / self.collectionView.bounds.size.width;
        if (tapGesture.view == label) {
            label.font = [UIFont systemFontOfSize:18];
            label.textColor = [UIColor colorWithHex:0x0073dd];
            [self.collectionView setContentOffset:CGPointMake(index * self.collectionView.bounds.size.width, 0) animated:YES];
            self.imageView.image = [UIImage imageNamed:@"my_select2"];
        } else {
            label.textColor = [UIColor colorWithHex:0x676767];
            label.font = [UIFont systemFontOfSize:14];
            self.imageView.image = [UIImage imageNamed:@"my_select1"];
        }
        
    }
    
}



- (void)setupCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    
    collectionView.dataSource = self;
    //    collectionView.delegate = self;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:collectionViewCellID];
    collectionView.showsVerticalScrollIndicator = false;
    collectionView.showsHorizontalScrollIndicator = false;
    collectionView.pagingEnabled = true;
    collectionView.bounces = false;
    
    //自动布局
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.topView.mas_bottom);
    }];
    
    //全局赋值
    self.collectionView = collectionView;
    
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.itemSize = self.collectionView.frame.size;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCellID forIndexPath:indexPath];
    UIViewController *vc= [[DYZShowController alloc]init];
    [self addChildViewController:vc];
    [cell.contentView addSubview:vc.view];
    cell.backgroundColor = [UIColor randomColor];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - UICollectionViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    CGFloat ratio = scrollView.contentOffset.x / scrollView.frame.size.width;
    //    NSLog(@"%f",ratio);
    ////    UILabel *label1 = [self.topView.subviews objectAtIndex:1];
    //    for(int i=0; i<self.labelArr.count; i++)
    //    {
    //        UILabel *label = self.labelArr[i];
    //        if(ratio>=0.5)
    //        {
    //            label.font = [UIFont systemFontOfSize:18];
    //            label.textColor = [UIColor colorWithHex:0x0073dd];
    //            self.imageView.image = [UIImage imageNamed:@"my_select1"];
    //        }
    //        else
    //        {
    //            label.textColor = [UIColor colorWithHex:0x676767];
    //            label.font = [UIFont systemFontOfSize:14];
    //            self.imageView.image = [UIImage imageNamed:@"my_select2"];
    //        }
    
    //        NSInteger index = (tapGesture.view.frame.origin.x * 2) / self.collectionView.bounds.size.width;
    //        if (tapGesture.view == label) {
    //            label.font = [UIFont systemFontOfSize:18];
    //            label.textColor = [UIColor colorWithHex:0x0073dd];
    //            [self.collectionView setContentOffset:CGPointMake(index * self.collectionView.bounds.size.width, 0) animated:YES];
    //            self.imageView.image = [UIImage imageNamed:@"my_select2"];
    //        } else {
    //            label.textColor = [UIColor colorWithHex:0x676767];
    //            label.font = [UIFont systemFontOfSize:14];
    //            self.imageView.image = [UIImage imageNamed:@"my_select1"];
    //        }
    
    //    }
    
}


@end
